﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.HashUtility
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Security.Cryptography;

namespace Microsoft.InfoCards
{
  internal static class HashUtility
  {
    private static object s_lock = new object();
    private static SHA256Managed s_hasher = new SHA256Managed();
    public const string HashAlgorithmName = "SHA256";

    public static int HashBitLength
    {
      get
      {
        return HashUtility.s_hasher.HashSize;
      }
    }

    public static int HashBufferLength
    {
      get
      {
        return HashUtility.HashBitLength / 8;
      }
    }

    public static void SetHashValue(byte[] data, int dataIndex, byte[] dataToHash)
    {
      if (dataToHash == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (dataToHash));
      HashUtility.SetHashValue(data, dataIndex, dataToHash, 0, dataToHash.Length);
    }

    public static void SetHashValue(
      byte[] data,
      int dataIndex,
      byte[] dataToHash,
      int dataToHashIndex,
      int dataToHashSize)
    {
      if (data == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (data));
      if (dataIndex < 0 || dataIndex >= data.Length)
        throw InfoCardTrace.ThrowHelperError((Exception) new ArgumentOutOfRangeException(nameof (dataIndex), (object) dataIndex, SR.GetString("StoreHashUtilityDataOutOfRange")));
      if (dataToHash == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (dataToHash));
      if (dataToHashIndex < 0 || dataToHashIndex > dataToHash.Length)
        throw InfoCardTrace.ThrowHelperError((Exception) new ArgumentOutOfRangeException(nameof (dataToHashIndex), (object) dataToHashIndex, SR.GetString("StoreHashUtilityDataToHashOutOfRange")));
      if (dataToHashSize < 0 || dataToHashSize > dataToHash.Length - dataToHashIndex)
        throw InfoCardTrace.ThrowHelperError((Exception) new ArgumentOutOfRangeException(nameof (dataToHashIndex), (object) dataToHashIndex, SR.GetString("StoreHashUtilityDataToHashOutOfRange")));
      byte[] numArray = (byte[]) null;
      lock (HashUtility.s_lock)
        numArray = HashUtility.s_hasher.ComputeHash(dataToHash, dataToHashIndex, dataToHashSize);
      Array.Copy((Array) numArray, 0, (Array) data, dataIndex, numArray.Length);
      Array.Clear((Array) numArray, 0, numArray.Length);
    }
  }
}
